from flask import Flask

app = Flask(__name__)
books = {
    1: "Python编程：从入门到实践",
    2: "流畅的Python",
    3: "深入理解计算机系统",
    4: "算法导论",
    5: "数据结构与算法分析",
    6: "机器学习实战",
    7: "深度学习入门：基于Python的理论与实现",
    8: "设计模式：可复用面向对象软件的基础",
}


@app.route("/")
# 视图函数/端点函数，view function/endpoint function，不能重复
def hello():
    return "Hello, Flask12345!"


@app.route("/homepage")
@app.route("/first")
def first():
    return "<b>Hello, this is home page!</b>"


# @app.route("/page/<int:page_id>", methods=["GET"])
@app.get("/page/<int:page_id>")
def page(page_id):
    print(type(page_id))
    return f"<b>Hello, this is page {page_id}!</b>"


def second():
    return "<b>Hello, this is second page!</b>"


@app.get("/books/<int:book_id>")
def get_book(book_id):
    book = books.get(book_id, "未知书籍")
    return f"<b>书籍ID: {book_id}, 书名: {book}</b>"


@app.get("/books")
def list_book():
    res = [f"ID: {book_id}, 书名: {book}" for book_id, book in books.items()]
    return res


app.add_url_rule("/second", view_func=second)

if __name__ == "__main__":
    app.run(debug=True, port=5011)
